<?php
namespace Apps\Backend;
use Phalcon\Flash\Direct as Flash;
class Module {
    public function registerAutoloaders(){
        $loader = new \Phalcon\Loader();
        $loader->registerNamespaces(array(
            'Apps\Backend\Controllers' =>APPLICATION_PATH.'/apps/backend/controllers/',
            'Apps\Backend\Models' => APPLICATION_PATH.'/apps/backend/models/',
            'Apps\Backend\Forms' => APPLICATION_PATH.'/apps/backend/forms/',
        ));
        $loader->register();
    }
    public function registerServices($di){
        $di->set('dispatcher', function() {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setDefaultNamespace("Apps\Backend\Controllers");
            return $dispatcher;
        });

        $di->set('flash',function () {
                return new Flash(array(
                    'error' => 'alert alert-dismissable alert-danger',
                    'success' => 'alert alert-dismissable alert-success',
                    'notice' => 'alert alert-dismissable alert-info',
                ));
            }
        );

        $di->set('view', function() {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir('../apps/backend/views/');
            $view->registerEngines(array(
                ".volt" => function($view, $di) {
                        $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
                        $volt->setOptions(array(
                            "compiledPath" => APPLICATION_PATH."/apps/cache/volt/",
                            "compiledExtension" => ".compiled",
                            'compiledSeparator' => '_',
                            'compileAlways' => true,
                        ));

                        $volt->getCompiler()->addFunction('_', function ($resolvedArgs, $exprArgs) use ($di) {
                            return sprintf('$this->translate->query(\'%s\')', $exprArgs[0]['expr']['value']);
                        });
                        $volt->getCompiler()->addFunction('numberFormat','number_format');
                        $volt->getCompiler()->addFunction('strrepeat', 'str_repeat');
                        $volt->getCompiler()->addFunction('strpos', 'strpos');
                        $volt->getCompiler()->addFunction('unserialize', 'unserialize');
                        $volt->getCompiler()->addFunction('array_search','array_search');
                        $volt->getCompiler()->addFunction('strlen','strlen');
                        $volt->getCompiler()->addFunction('count','count');
                        return $volt;
                    }
            ));
            return $view;
        });
    }
}